import 'package:flutter/material.dart';

class CollectPage extends StatefulWidget {
  const CollectPage({super.key});

  @override
  State<CollectPage> createState() => _CollectPageState();
}

class _CollectPageState extends State<CollectPage> {
  final List<Map<String, dynamic>> _items = [];
  int _page = 1;
  bool _hasMore = true;
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    _loadData();
    _scrollController.addListener(_scrollListener);
  }

  Future<void> _loadData({bool refresh = false}) async {
    final newItems = List.generate(15, (i) => {
      'id': i + (_page-1)*15,
      'title': 'Flutter实战：${['状态管理', '路由导航', '网络请求'][i%3]}',
      'author': '作者 ${['张三','李四','王五'][i%3]}',
      'time': '2023-08-0${i+1} 14:${i*5}',
      'avatar': 'https://picsum.photos/50/50?random=$i'
    });
  }

  void _scrollListener() {
    // 滚动到底部检测
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('收藏列表'),
      ),
      body: RefreshIndicator(
        onRefresh: () => _loadData(refresh: true),
        child: ListView.builder(
          controller: _scrollController,
          itemCount: _items.length + 1,
          itemBuilder: (context, index) {
            return Card(
              margin: EdgeInsets.all(8),
              child: Padding(
                padding: EdgeInsets.all(12),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        CircleAvatar(
                          backgroundImage: NetworkImage(_items[index]['avatar']),
                          radius: 20,
                        ),
                        SizedBox(width: 12),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(_items[index]['author'], style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 16
                            )),
                            Text(_items[index]['time'], style: TextStyle(
                              fontSize: 12,
                              color: Colors.grey[600]
                            ))  
                          ],
                        )
                      ],
                    ),
                    SizedBox(height: 10),
                    Text(_items[index]['title'], style: TextStyle(
                      fontSize: 18,
                      color: Colors.blueGrey[800]
                    )),
                    ButtonBar(
                      children: [
                        IconButton(
                          icon: Icon(Icons.favorite_border),
                          onPressed: () {},
                        ),
                        IconButton(
                          icon: Icon(Icons.share),
                          onPressed: () {},
                        )
                      ],
                    )
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
